var app = angular.module('selectApp', []);
var transform = function (data) {
    return $.param(data);
}, postCfg = {
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    },
    transformRequest: transform
};
app.directive('selectOption', function ($http) {
    return {
        restrict: 'E',
        scope: true,
        link: function (scope, element, attrs) {
        	setTimeout(function(){
	            scope.list = attrs.grup;
	            var data = {
	                'groupName': attrs.title
	            };
	            $http.post('/datadicItem/findItemsByGroupName', data, postCfg)
	                .success(function (resp) {
	                	scope.title = attrs.title;
	                    scope.list = resp;
	                    scope.grup = parseInt(attrs.grup);
	                });
        	},100);
        },
        template: 
        '<select  class="select-box radius"' +
        'ng-model="grup"' +
        'ng-change="getTypeVlue(title,grup,item.id)"' +
        'ng-options="act.id as act.itemName for act in list  | filter:find">' + '<option value="">' + '--请选择--' + '</option>' +
        '</select>',
        replace: true,
    };
});
app.directive('selectOptionrepair', function ($http) {
    return {
        restrict: 'E',
        scope: true,
        link: function (scope, element, attrs) {
        	setTimeout(function(){
	            scope.list = attrs.grup;
	            var data = {
	                'groupName': attrs.title
	            };
	            $http.post('/datadicItem/findItemsByGroupName', data, postCfg)
	                .success(function (resp) {
	                	scope.title = attrs.title;
	                    scope.list = resp;
	                    scope.grup = parseInt(attrs.grup);
	                });
        	},100);
        },
        template: 
        '<select  class="select-box radius"' +
        'ng-model="grup"' +
        'ng-change="getTypeVlue(title,grup,item.id)"' +
        'ng-options="act.id as act.itemName for act in list | filter:find ">' + '<option value="">' + '--请选择--' + '</option>' +
        '</select>',
        replace: true,
    };
});
app.directive('showContent',function($http){
	return {
		restrict:'E',
		scope : {
	        customerKey: '=key',
	      },
		link:function(scope, element, attrs) {
	         var data = {
					 'id':attrs.itemId
			};
	         scope.$watch(attrs.itemId,function(newValue,oldValue){ 
	        	 $http.post('/datadicItem/getItemById',data,postCfg)
	              .success(function(resp){
	            	  scope.content= resp.itemName;
	             });
	         });
	        },
		template:'<span ng-bind="content"></span>',
		replace:true
	};
});
app.directive('defLaydate', function($timeout) {
    return {
        require: '?ngModel',
        restrict: 'A',
        scope: {
            	ngModel: '=',
                maxDate:'@',
                minDate:'@'
        },
        link: function(scope, element, attr, ngModel) {
            var _date = null,_config={};
            $timeout(function(){ 
                // 初始化参数 
            _config = {
                elem: '#' + attr.id,
                format: attr.format != undefined && attr.format != '' ? attr.format : 'YYYY-MM-DD',
                max:attr.hasOwnProperty('maxDate')?attr.maxDate:'',
                min:attr.hasOwnProperty('minDate')?attr.minDate:'',
                choose: function(data) {
               	 ngModel.$setViewValue(element.val())
                },
                clear:function(){
                   ngModel.$setViewValue(null);
                }
            };
            // 初始化
            _date= laydate(_config);
            
            // 监听日期最大值
            if(attr.hasOwnProperty('maxDate')){
                attr.$observe('maxDate', function (val) {
                    _config.max = val;
                });
            }
            // 监听日期最小值
            if(attr.hasOwnProperty('minDate')){
               attr.$observe('minDate', function (val) {
                    _config.min = val;
                });
            }
           
            // 模型值同步到视图上
            ngModel.$render = function() {
                element.val(ngModel.$viewValue || '');
            };

            // 监听元素上的事件
            element.on('blur keyup change', function() {
                scope.$apply(setVeiwValue);
            });

            setVeiwValue();

            // 更新模型上的视图值
            function setVeiwValue() {
                var val = element.val();
                ngModel.$setViewValue(val);
            }
        },0); 
        }
    };
});





	  